<?php

class Ialbum_Widget_MostLikedAlbumsController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
  	$likeTable  = Engine_Api::_()->getDbtable('likes', 'core');
  	$lName = $likeTable->info('name');
  	$albumTable = Engine_Api::_()->getItemTable('album');
  	$aName = $albumTable->info('name');
  	$select = $albumTable->select()	
  				->from($aName)
  				->join($lName, $lName. '.resource_id = ' . $aName .'.album_id AND resource_type="album"', '')
  				->where($aName .'.search = 1')
  				->group($aName .'.album_id')
  				->order("count($aName.album_id) DESC");
	
	$this->view->paginator = $paginator = Zend_Paginator::factory($select);
    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 5));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Do not render if nothing to show
    if( $paginator->getTotalItemCount() <= 0 ) {
      return $this->setNoRender();
    }
  }
}